package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.domain.DestinationTemplate;
import cn.wolfcode.luowowo.search.domain.StrategyTemplate;
import cn.wolfcode.luowowo.search.domain.TravelTemplate;
import cn.wolfcode.luowowo.search.domain.UserInfoTemplate;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.serivce.*;
import cn.wolfcode.luowowo.search.vo.SearchResult;
import cn.wolfcode.luowowo.website.web.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class IndexController {

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private ITravelCommendService travelCommendService;

    @Reference
    private ITravelService travelService;

    @Reference
    private IDestinationSearchService destinationSearchService;

    @Reference
    private IStrategySearchService strategySearchService;

    @Reference
    private ITravelSearchService travelSearchService;

    @Reference
    private IUserInfoSearchService userInfoSearchService;

    @Reference
    private ISearchService searchService;

    //搜索
    @RequestMapping("/q")
    public String search(Model model,@ModelAttribute("qo")SearchQueryObject qo){

        switch (qo.getType()){
            case SearchQueryObject.SEARCH_TYPE_DEST: //目的地
                return searchDest(model,qo);
            case SearchQueryObject.SEARCH_TYPE_STRATEGY: //攻略
                return searchStrategy(model,qo);
            case SearchQueryObject.SEARCH_TYPE_TRAVEL: //游记
                return searchTravel(model,qo);
            case SearchQueryObject.SEARCH_TYPE_USER: //用户
                return searchUser(model,qo);
            default:return searchAll(model,qo);
        }
    }

    //目的地搜索
    private String searchDest(Model model, SearchQueryObject qo) {
        //根据关键字查询es中的目的地对象
        DestinationTemplate dest = destinationSearchService.findByName(qo.getKeyword());
        //如果根据关键字查询出来的目的地不为空,查询目的地下所有的攻略,游记,用户
        if(dest != null){
            SearchResult result = new SearchResult();
            List<StrategyTemplate> ss = strategySearchService.findByDestName(dest.getName());//攻略
            result.setTotal(result.getTotal() + ss.size());
            //约定只要前5个 左闭右开
            if(ss.size() > 5){
                ss = ss.subList(0,5);
            }
            List<TravelTemplate> ts = travelSearchService.findByDestName(dest.getName());//游记
            result.setTotal(result.getTotal() + ts.size());
            if(ts.size() > 5){
                ts = ts.subList(0,5);
            }
            List<UserInfoTemplate> us = userInfoSearchService.findByDestName(dest.getName());//用户
            result.setTotal(result.getTotal() + us.size());
            if(us.size() > 5){
                us = us.subList(0,5);
            }

            result.setStrategys(ss);
            result.setTravels(ts);
            result.setUsers(us);
            model.addAttribute("data",result);
        }
        //如果没有,提示查不到
        model.addAttribute("dest",dest);

        return "index/searchDest";
    }

    //搜索全部
    private String searchAll(Model model, SearchQueryObject qo) {
        SearchResult result = new SearchResult();

        Page<DestinationTemplate> ds = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME,
                DestinationTemplate.class, qo, "name", "info");
        Page<UserInfoTemplate> us = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");
        Page<TravelTemplate> ts = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");
        Page<StrategyTemplate> ss = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle","summary");

        result.setTotal(ds.getTotalElements() + us.getTotalElements() + ts.getTotalElements() + ss.getTotalElements());
        result.setDests(ds.getContent());
        result.setUsers(us.getContent());
        result.setTravels(ts.getContent());
        result.setStrategys(ss.getContent());

        model.addAttribute("data",result);
        return "index/searchAll";
    }

    //搜索用户
    private String searchUser(Model model, SearchQueryObject qo) {

        Page<UserInfoTemplate> us = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");

        model.addAttribute("page",us);

        return "index/searchUser";
    }

    //搜索游记
    private String searchTravel(Model model, SearchQueryObject qo) {

        Page<TravelTemplate> ts = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");

        model.addAttribute("page",ts);

        return "index/searchTravel";
    }

    //搜索攻略
    private String searchStrategy(Model model, SearchQueryObject qo) {

        Page<StrategyTemplate> ss = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle","summary");

        model.addAttribute("page",ss);

        return "index/searchStrategy";
    }



    @RequestMapping("")
    public String index(Model model){

        //tcs
        model.addAttribute("tcs",travelCommendService.getTop5());

        //sc
        model.addAttribute("sc",strategyCommendService.getTop5().get(0));

        return "index/index";
    }

    //游记分页
    @RequestMapping("/index/travelPage")
    public String travelPage(Model model, @ModelAttribute("qo")TravelQuery qo){

        model.addAttribute("pageInfo",travelService.query(qo));

        return "index/travelPageTpl";
    }
}
